package com.ruoyi.report_tg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_tg.domain.bo.TdsslyjzjlbBsInfoBo;
import com.ruoyi.report_tg.domain.vo.TdsslyjzjlbBsInfoVo;
import com.ruoyi.report_tg.domain.TdsslyjzjlbBsInfo;
import com.ruoyi.report_tg.mapper.TdsslyjzjlbBsInfoMapper;
import com.ruoyi.report_tg.service.ITdsslyjzjlbBsInfoService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 土的烧失量、有机质、酸碱度、易熔盐记录Service业务层处理
 *
 * @author zyb
 * @date 2024-05-24
 */
@RequiredArgsConstructor
@Service
@Transactional
public class TdsslyjzjlbBsInfoServiceImpl implements ITdsslyjzjlbBsInfoService {

    private final TdsslyjzjlbBsInfoMapper baseMapper;

    /**
     * 查询土的烧失量、有机质、酸碱度、易熔盐记录
     */
    @Override
    @FIllBasicInfo
    public TdsslyjzjlbBsInfoVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }

    /**
     * 查询土的烧失量、有机质、酸碱度、易熔盐记录列表
     */
    @Override
    public TableDataInfo<TdsslyjzjlbBsInfoVo> queryPageList(TdsslyjzjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TdsslyjzjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<TdsslyjzjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询土的烧失量、有机质、酸碱度、易熔盐记录列表
     */
    @Override
    public List<TdsslyjzjlbBsInfoVo> queryList(TdsslyjzjlbBsInfoBo bo) {
        LambdaQueryWrapper<TdsslyjzjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TdsslyjzjlbBsInfo> buildQueryWrapper(TdsslyjzjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TdsslyjzjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, TdsslyjzjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, TdsslyjzjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), TdsslyjzjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), TdsslyjzjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), TdsslyjzjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), TdsslyjzjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), TdsslyjzjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), TdsslyjzjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), TdsslyjzjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, TdsslyjzjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), TdsslyjzjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), TdsslyjzjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), TdsslyjzjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), TdsslyjzjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), TdsslyjzjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), TdsslyjzjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), TdsslyjzjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(bo.getZhuoswd() != null, TdsslyjzjlbBsInfo::getZhuoswd, bo.getZhuoswd());
        lqw.eq(bo.getTuyzl1() != null, TdsslyjzjlbBsInfo::getTuyzl1, bo.getTuyzl1());
        lqw.eq(bo.getTuyzl2() != null, TdsslyjzjlbBsInfo::getTuyzl2, bo.getTuyzl2());
        lqw.eq(bo.getDi1ccl1() != null, TdsslyjzjlbBsInfo::getDi1ccl1, bo.getDi1ccl1());
        lqw.eq(bo.getDi1ccl2() != null, TdsslyjzjlbBsInfo::getDi1ccl2, bo.getDi1ccl2());
        lqw.eq(bo.getDi2ccl1() != null, TdsslyjzjlbBsInfo::getDi2ccl1, bo.getDi2ccl1());
        lqw.eq(bo.getDi2ccl2() != null, TdsslyjzjlbBsInfo::getDi2ccl2, bo.getDi2ccl2());
        lqw.eq(bo.getDi3ccl1() != null, TdsslyjzjlbBsInfo::getDi3ccl1, bo.getDi3ccl1());
        lqw.eq(bo.getDi3ccl2() != null, TdsslyjzjlbBsInfo::getDi3ccl2, bo.getDi3ccl2());
        lqw.eq(bo.getKongggzl1() != null, TdsslyjzjlbBsInfo::getKongggzl1, bo.getKongggzl1());
        lqw.eq(bo.getKongggzl2() != null, TdsslyjzjlbBsInfo::getKongggzl2, bo.getKongggzl2());
        lqw.eq(bo.getShaosl1() != null, TdsslyjzjlbBsInfo::getShaosl1, bo.getShaosl1());
        lqw.eq(bo.getShaosl2() != null, TdsslyjzjlbBsInfo::getShaosl2, bo.getShaosl2());
        lqw.eq(bo.getPingjsll() != null, TdsslyjzjlbBsInfo::getPingjsll, bo.getPingjsll());
        return lqw;
    }

    /**
     * 新增土的烧失量、有机质、酸碱度、易熔盐记录
     */
    @Override
    public Boolean insertByBo(TdsslyjzjlbBsInfoBo bo) {
        TdsslyjzjlbBsInfo add = BeanUtil.toBean(bo, TdsslyjzjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改土的烧失量、有机质、酸碱度、易熔盐记录
     */
    @Override
    public Boolean updateByBo(TdsslyjzjlbBsInfoBo bo) {
        TdsslyjzjlbBsInfo update = BeanUtil.toBean(bo, TdsslyjzjlbBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TdsslyjzjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除土的烧失量、有机质、酸碱度、易熔盐记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
